import { messages } from "share/common";
import React, { Component } from "react";
import { Button, Checkbox, Spin, message } from "antd";
const Group = Checkbox.Group;

import HotelServer from "../hotel-server";

class SetFeePush extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      feePush: [],
      hotelData: {}, // 酒店基本设置数据
    };
  }

  componentWillMount() {
    this.initData();
  }

  formatFeePush = (value) => {
    let pushValue = [];
    switch (value) {
      case 1001:
        pushValue = [1001];
        break;
      case 1002:
        pushValue = [1002];
        break;
      case 1003:
        pushValue = [1001, 1002];
        break;
      default:
        break;
    }
    this.setState({ feePush: pushValue });
  };

  initData = () => {
    HotelServer(this)
      .getPlatformModelData({ companyOid: this.props.companyOID })
      .then((res) => {
        this.formatFeePush(res.data.body.expensePushType);
        this.setState({ hotelData: res.data.body });
      });
  };

  onFeePushChange = (value) => {
    this.setState({ feePush: value });
  };

  onSaveSetting = () => {
    const { hotelData, feePush } = this.state;
    const params = Object.assign({}, hotelData);
    params.companyOid = this.props.companyOID;
    if (feePush.length > 0) {
      if (feePush.length === 1) {
        params.expensePushType = feePush[0];
      } else {
        params.expensePushType = 1003;
      }
    }
    HotelServer(this)
      .savePlatformModelData(params)
      .then((res) => {
        if (res.data.success) {
          this.initData();
          message.success(messages("supplier-1.key355") /*保存成功*/);
        } else {
          message.error(res.data.message);
        }
      });
  };

  render() {
    const { feePush, loading } = this.state;
    return (
      <div className="airline-config-container">
        <Spin spinning={loading}>
          <h2>{messages("supplier-1.key486") /*因公订单-推费用设置*/}</h2>
          <a className="a-line-bottom"></a>
          <Group value={feePush} onChange={this.onFeePushChange}>
            <Checkbox value={1002}>{messages("supplier-1.key487") /*公司支付推费用*/}</Checkbox>
            <Checkbox value={1001}>{messages("supplier-1.key264") /*个人支付*/}</Checkbox>
          </Group>
          <br />
          <br />
          <Button type="primary" onClick={this.onSaveSetting}>
            {messages("supplier-1.key356") /*保存配置*/}
          </Button>
        </Spin>
      </div>
    );
  }
}

export default SetFeePush;
